/**
 * 
 */
package au.edu.swin.videoshop.posapp.ui.sections;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.ejb.EJB;
import javax.faces.component.UIData;
import javax.faces.component.UIViewRoot;
import javax.faces.context.FacesContext;

import au.edu.swin.videoshop.bean.Customer;
import au.edu.swin.videoshop.bean.Rental;
import au.edu.swin.videoshop.service.RentalService;
import au.edu.swin.videoshop.service.TitleService;

/**
 * @author julie
 *
 */
public class ReturnSection implements Serializable {
	private String mTitleCopyId;
	private Customer mCustomer;
	private List mRentals;
	private Map mRentalById;
	private BigDecimal mTotalPrice = BigDecimal.ZERO;
	
	@EJB
	private RentalService mRentalService;
	
	@EJB
	private TitleService mTitleService;
	
	public ReturnSection() {
	}

	/**
	 * @return the rentals
	 */
	public List getRentals() {
		return mRentals;
	}

	/**
	 * @return the customer
	 */
	public Customer getCustomer() {
		return mCustomer;
	}

	/**
	 * @return the totalPrice
	 */
	public BigDecimal getTotalPrice() {
		return mTotalPrice;
	}

	/**
	 * @return the titleCopyId
	 */
	public String getTitleCopyId() {
		return mTitleCopyId;
	}

	/**
	 * @param titleCopyId the titleCopyId to set
	 */
	public void setTitleCopyId(String titleCopyId) {
		mTitleCopyId = titleCopyId;
	}

	public String add() {
		if (mTitleCopyId != null) {
			FacesContext facesContext = FacesContext.getCurrentInstance();
			UserSection userSection = (UserSection)facesContext.getApplication().evaluateExpressionGet(facesContext, "#{userSection}", UserSection.class);

			Rental rental = mRentalService.findRentalByTitleCopyId(mTitleCopyId, new Date());
			
			if (rental != null) {
				if (mCustomer == null) {
					mCustomer = rental.getCustomer();
				}
				
				if (rental.getCustomer().getCustomerId().equals(mCustomer.getCustomerId())) {
					if (mRentals == null) {
						mRentals = new ArrayList();
					}
					if (mRentalById == null) {
						mRentalById = new HashMap();
					}
					
					if (!mRentalById.containsKey(rental.getRentalId()) && rental.getTitleCopy().getStore().getStoreId().equals(userSection.getStaff().getStore().getStoreId())) {
						mRentals.add(rental);
						mRentalById.put(rental.getRentalId(), rental);
						mTotalPrice = mTotalPrice.add(rental.getFinalCharge());
					}
				}
			}
			
			mTitleCopyId = null;
		}
		return null;
	}

	
	public String delete() {
		FacesContext facesContext = FacesContext.getCurrentInstance();
		UIViewRoot root = facesContext.getViewRoot();
		UIData table = (UIData)root.findComponent("returnForm").findComponent("returnTable");
		Rental rental = (Rental) table.getRowData();
		
		mRentals.remove(rental);
		mRentalById.remove(rental.getRentalId());
		mTotalPrice = mTotalPrice.subtract(rental.getFinalCharge()); 
		
		return null;
	}

	public String finish() {
		if (mRentals != null && !mRentals.isEmpty()) {
			for (Iterator i = mRentals.iterator(); i.hasNext();) {
				mRentalService.addReturn(((Rental)i.next()).getRentalId(), new Date());
			}
			
			clear();
			
			return "finish";
		}
		
		return null;
	}
	
	public String cancel() {
		clear();
		
		return null;
	}
	
	public String clear() {
		mCustomer = null;
		mTitleCopyId = null;
		mRentals = null;
		mRentalById = null;
		mTotalPrice = BigDecimal.ZERO;
		
		return null;
	}
}

